package com.lagou.service.impl;

import com.lagou.dao.RoleMapper;
import com.lagou.domain.*;
import com.lagou.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 角色列表查询&条件查询
     *
     * @param role
     * @return
     */
    @Override
    public List<Role> findAllRole(Role role) {

        return roleMapper.findAllRole(role);
    }

    /**
     * 新增角色
     *
     * @param role
     */
    @Override
    public void saveRole(Role role) {

        Date date = new Date();
        role.setUpdatedTime(date);
        role.setCreatedTime(date);
        role.setCreatedBy("save");
        role.setUpdatedBy("save");
        roleMapper.saveRole(role);
    }

    /**
     * 修改角色
     *
     * @param role
     */
    @Override
    public void updateRole(Role role) {

        role.setUpdatedBy("update");
        role.setUpdatedTime(new Date());
        roleMapper.updateRole(role);
    }

    /**
     * 为角色分配菜单列表
     *
     * @param roleMenuVO
     */
    @Override
    public void roleContextMenu(RoleMenuVO roleMenuVO) {

        roleMapper.deleteRoleContextMenu(roleMenuVO.getRoleId());
        Date date = new Date();
        for (Integer menuId : roleMenuVO.getMenuIdList()) {

            Role_menu_relation roleMenuRelation = new Role_menu_relation();
            roleMenuRelation.setMenuId(menuId);
            roleMenuRelation.setRoleId(roleMenuVO.getRoleId());
            roleMenuRelation.setCreatedTime(date);
            roleMenuRelation.setCreatedBy("system");
            roleMenuRelation.setUpdatedTime(date);
            roleMenuRelation.setUpdatedBy("system");
            roleMapper.roleContextMenu(roleMenuRelation);
        }
    }

    /**
     * 删除角色
     *
     * @param id 角色id
     */
    @Override
    public void deleteRole(Integer id) {

        roleMapper.deleteRoleContextMenu(id);
        roleMapper.deleteRole(id);
    }

    /**
     * 为角色分配资源
     * @param roleResourceVO
     */
    @Override
    public void roleContextResource(RoleResourceVO roleResourceVO) {

        roleMapper.deleteRoleContextResource(roleResourceVO.getRoleId());
        List<Integer> resourceIdList = roleResourceVO.getResourceIdList();
        Date date = new Date();
        for (Integer resourceId : resourceIdList) {

            RoleResourceRelation roleResourceRelation = new RoleResourceRelation();
            roleResourceRelation.setResourceId(resourceId);
            roleResourceRelation.setRoleId(roleResourceVO.getRoleId());
            roleResourceRelation.setCreatedBy("system");
            roleResourceRelation.setCreatedTime(date);
            roleResourceRelation.setUpdatedBy("system");
            roleResourceRelation.setUpdatedTime(date);
            roleMapper.roleContextResource(roleResourceRelation);
        }
    }
}
